package com.example.dayday.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.dayday.common.R;
import com.example.dayday.dto.MessageDto;
import com.example.dayday.pojo.*;
import com.example.dayday.service.*;
import com.example.dayday.vo.StudentVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Helen
 * @since 2022-09-07
 */
@RestController
@RequestMapping("/message")
public class MessageController {


    @Autowired
    private  MessageService messageService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private TeacherCourseService teacherCourseService;

    @GetMapping("/getMsgNmber")
    public R getMsgNmber(HttpSession session){
        User role = (User) session.getAttribute("user");
        if(role.getRole()==1){
            Student user = (Student) role;
            Long studentId = user.getStudentId();
            LambdaQueryWrapper<Message> wq = new LambdaQueryWrapper<>();
            wq.eq(Message::getStudentId,studentId);
            List<Message> list = messageService.list(wq);
            int i=0;
            for(Message m:list){
                if(m.getMsg().equals("1")){
                    i++;
                }
            }
            return R.ok().data("msgNmber",i);
        }
        return  R.error().message("当前登录不是学生");
    }

    @ApiOperation(value = "")
    @GetMapping("/getAll")
    public R getAll(HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user.getRole()==2){
            return R.error().message("当前登录不是学生");
        }
        Student s = (Student) user;
        LambdaQueryWrapper<Message> we = new LambdaQueryWrapper<>();
        we.eq(Message::getStudentId,s.getStudentId());
        List<Message> list = messageService.list(we);
        List<MessageDto> messageDtoList = new ArrayList<>();

        for (Message message : list) {
            MessageDto messageDto = new MessageDto();
            BeanUtils.copyProperties(message,messageDto);
            //学生姓名
            LambdaQueryWrapper<Student> studentW = new LambdaQueryWrapper<>();
            studentW.eq(Student::getStudentId,messageDto.getStudentId());
            Student student = studentService.getOne(studentW);
            messageDto.setStudentName(student.getStudentName());
            //课程名字 时间
            LambdaQueryWrapper<Course> courseW = new LambdaQueryWrapper<>();
            courseW.eq(Course::getCourseId,messageDto.getCourseId());
            Course course = courseService.getOne(courseW);
            messageDto.setCourseName(course.getCourseName());

            int year = course.getClassTime().getYear();
            Month month = course.getClassTime().getMonth();
            int day = course.getClassTime().getDayOfMonth();

            String startTime = year+"/"+month.getValue()+"/"+day;

            int year1 = course.getEndTime().getYear();
            Month month1 = course.getEndTime().getMonth();
            int day1 = course.getEndTime().getDayOfMonth();

            String endTime = year1+"/"+month1.getValue()+"/"+day1;

            messageDto.setBeginTime(startTime);
            messageDto.setEndTime(endTime);

            //主讲老师
            LambdaQueryWrapper<Teacher> teacherMainW = new LambdaQueryWrapper<>();
            teacherMainW.eq(Teacher::getTeacherId,course.getMainTeacherId());
            Teacher teacherMain = teacherService.getOne(teacherMainW);
            messageDto.setCourseMainTeacher(teacherMain.getTeacherName());

            //任课老师
            LambdaQueryWrapper<TeacherCourse> teacherCourseLambdaQueryWrapper = new LambdaQueryWrapper<>();
            teacherCourseLambdaQueryWrapper.eq(TeacherCourse::getCourseId,course.getCourseId());
            List<TeacherCourse> teacherCourseList = teacherCourseService.list(teacherCourseLambdaQueryWrapper); //任课老师列表 id=13, teacherId=202200005, courseId=1, courseName=null

            if(teacherCourseList!=null&&teacherCourseList.size()!=0){ //如果有任课老师
                List<Long> teacherIds = teacherCourseList.stream().map(teacherCourse -> {
                    return teacherCourse.getTeacherId();
                }).collect(Collectors.toList());

                LambdaQueryWrapper<Teacher> teacherLambdaQueryWrapper = new LambdaQueryWrapper<>();
                teacherLambdaQueryWrapper.in(Teacher::getTeacherId,teacherIds);
                List<Teacher> teacherList = teacherService.list(teacherLambdaQueryWrapper);

                StringBuffer teacherListStr = new StringBuffer();
                for (Teacher teacher : teacherList) {
                    teacherListStr.append(teacher.getTeacherName()+" ");
                }

                messageDto.setTeacherList(teacherListStr.toString());
            }
            messageDtoList.add(messageDto);
        }
        LambdaUpdateWrapper<Message> weu = new LambdaUpdateWrapper<>();
        weu.eq(Message::getStudentId,s.getStudentId());
        weu.set(Message::getMsg,"0");
        messageService.update(weu);
        return  R.ok().data("list",messageDtoList);
    }
}

